﻿#include "LocalDialog.h"

LocalDialog::LocalDialog(QWidget *parent)
    : QDialog(parent),
      localConnectionRadioButton(new QRadioButton("本地连接")),
      localDbNameEdit(new QLineEdit),
      localLabel(new QLabel("数据库名称")),
      connectButton(new QPushButton("确定连接")),
      cancelButton(new QPushButton("取消"))
{
    setWindowTitle("本地连接");
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // Update the stylesheet to increase the width of the QLineEdit
    setStyleSheet(
        "QLineEdit {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  color: gray;"
        "  width: 300px;"  // 增加编辑框的长度
        "}"
        "QPushButton {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  background-color: #0093D1;"
        "  color: white;"
        "}");

    mainLayout->addWidget(localConnectionRadioButton);

    // Create a horizontal layout for the label and edit box
    QHBoxLayout *dbNameLayout = new QHBoxLayout;
    dbNameLayout->addWidget(localLabel);
    dbNameLayout->addWidget(localDbNameEdit);
    mainLayout->addLayout(dbNameLayout);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(connectButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    connect(localConnectionRadioButton, &QRadioButton::toggled, this, &LocalDialog::onFieldsChanged);
    connect(connectButton, &QPushButton::clicked, this, &LocalDialog::accept);
    connect(cancelButton, &QPushButton::clicked, this, &LocalDialog::reject);
    connect(localDbNameEdit, &QLineEdit::textChanged, this, &LocalDialog::onFieldsChanged);

    setLayout(mainLayout);

    // Increase the width of the dialog
    setFixedWidth(400);  // 增加界面的宽度
}

QString LocalDialog::getLocalDbName() const
{
    return localDbNameEdit->text();
}

void LocalDialog::onFieldsChanged()
{
    connectButton->setEnabled(!localDbNameEdit->text().isEmpty());
}
